/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.service.impl;

import com.alibaba.fastjson.JSON;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxPage;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.file.excel.VtuzxExcel;
import com.vtuzx.core.file.excel.VtuzxExcelCell;
import com.vtuzx.core.file.excel.VtuzxExcelRow;
import com.vtuzx.core.file.excel.VtuzxExcelSheet;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.fast.dao.setting.DictDao;
import com.vtuzx.kingdee.xqc_api.config.XqcConfig;
import com.vtuzx.kingdee.xqc_api.service.IXqcTestRoomService;
import com.vtuzx.kingdee.xqc_core.bean.VtuzxArrayList;
import com.vtuzx.kingdee.xqc_core.dao.*;
import com.vtuzx.kingdee.xqc_core.service.IVtuzxKingdeeService;
import com.vtuzx.web.bean.VtuzxDownloadBean;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class XqcTestRoomServiceImpl implements IXqcTestRoomService {

    /** Logger */
    private static final Logger _logger = LoggerFactory.getLogger(XqcTestRoomServiceImpl.class);

    @Autowired
    private XqcConfig config;

    @Autowired
    TestRoomDao testRoomDao;

    @Autowired
    PurColDao purColDao;

    @Autowired
    OtherColDao otherColDao;

    @Autowired
    PrdProcessDao prdProcessDao;

    @Autowired
    CertificateDao certificateDao;

    @Autowired
    DictDao dictDao;

    @Autowired
    private IVtuzxKingdeeService kingdeeService;

    @Override
    public VtuzxMap getTestRoomList(ObjectId userId, Map<String, Object> param, int pageIndex, int pageSize) {
        String goodsType = VtuzxUtil.getString(param, "goodsType");
        String materialName = VtuzxUtil.getString(param, "materialName");
        String batchNo = VtuzxUtil.getString(param, "batchNo");
        String colUserName = VtuzxUtil.getString(param, "colUserName");
        List<String> colTime = VtuzxUtil.getObject(param, "colTime");

        Criteria cond = new Criteria();
        if (!VtuzxUtil.isEmpty(goodsType)) {
            cond.and("goodsType").is(goodsType);
        }
        if (!VtuzxUtil.isEmpty(colUserName)) {
            cond.and("colUserName").regex(colUserName);
        }
        if (!VtuzxUtil.isEmpty(materialName)) {
            cond.and("materialName").regex("^.*" + materialName + ".*$");
        }
        if (!VtuzxUtil.isEmpty(batchNo)) {
            cond.and("batchNo").regex(batchNo);
        }
        if (!VtuzxUtil.isEmpty(colTime)) {
            cond.and("colTime").gte(colTime.get(0)).lte(colTime.get(1) + " 23:59:59");
        }
        VtuzxPage page = testRoomDao.page(cond, new Document("createTime", -1), pageIndex, pageSize);
        List<VtuzxMap> dataList = page.iterator(itr -> {
            VtuzxMap data = new VtuzxMap(itr);
            data.append("id", VtuzxUtil.removeString(data, IVtuzxConst.Key__id));
            data.append(IVtuzxConst.Key_createTime, VtuzxUtil.formatYmdHmF(VtuzxUtil.removeObject(data, IVtuzxConst.Key_createTime)));
            data.remove(IVtuzxConst.Key_updateTime);
            return data;
        });
        for (VtuzxMap data: dataList) {
            List<String> picList = new ArrayList<>();
            List<String> pics = VtuzxUtil.getObject(data, "pics");
            if (!VtuzxUtil.isEmpty(pics) && pics.size() != 0) {
                for (String pic: pics) {
                    String certificateDateFolder = config.rootUrl() + "testRoom/" + pic;
                    picList.add(certificateDateFolder);
                }
            }
            data.append("picList", picList);
        }

        return new VtuzxMap(IVtuzxConst.Key_total, page.total()).append(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public void testQualified(ObjectId userId, String name, String billNo, String testMode) {
        testRoomDao.updateOne(Criteria.where("billNo").is(billNo),
                new Document("testUserId", userId)
                        .append("testUserName", name)
                        .append("testMode", testMode)
                        .append("status", "audit")
                        .append("testTime", VtuzxUtil.formatYmdHmsF(new Date()))
                        .append("testQualified", "qualified"));
        if (billNo.startsWith("CGSH")) {
            purColDao.updateOne(Criteria.where("billNo").is(billNo), new Document("check", true));
        } else {
            otherColDao.updateOne(Criteria.where("billNo").is(billNo), new Document("check", true));
        }
    }

    @Override
    public void uploadReport(ObjectId userId, String name, String billNo, List<VtuzxFile> vFileList, String picStr) {
        List<String> picList = new ArrayList<>();
        if (picStr != null) {
            picList = JSON.parseArray(picStr, String.class);
        }
        String dateFolder = VtuzxUtil.formatYmd(new Date());
        String testRoomRootFolder = config.rootPath() + "testRoom/";
        String testRoomDateFolder = testRoomRootFolder + dateFolder + "/";
        new File(testRoomDateFolder).mkdirs();
        List<String> uploadPics = new ArrayList<>();
        if (vFileList.size() != 0) {
            for (VtuzxFile vFile: vFileList) {
                String fileName = VtuzxUtil.random(8) + "." + VtuzxUtil.first(vFile.ext(), "jpg");
                vFile.file().renameTo(new File(testRoomDateFolder + fileName));
                uploadPics.add(dateFolder + "/" + fileName);
            }
        }

        Document testRoom = testRoomDao.findOne(Criteria.where("billNo").is(billNo));
        List<String> pics = VtuzxUtil.getObject(testRoom, "pics");
        if (VtuzxUtil.isEmpty(pics)) {
            testRoomDao.updateOne(Criteria.where("billNo").is(billNo), new Document("pics", uploadPics));
        } else {
            //循环原有图片
            for (String pic: pics) {
                if (!picList.contains(pic)) {
                    File delFile = new File(config.rootPath() + "testRoom/" + pic);
                    if (delFile.exists()) {
                        delFile.delete();
                    }
                }
            }
            picList.addAll(uploadPics);
            testRoomDao.updateOne(Criteria.where("billNo").is(billNo), new Document("pics", picList));
        }

//        VtuzxFile vFile = vFileList.get(0);
//        String dateFolder = VtuzxUtil.formatYmd(new Date());
//        String testroomRootFolder = config.rootPath() + "testroom/";
//        String testroomDateFolder = testroomRootFolder + dateFolder + "/";
//        new File(testroomDateFolder).mkdirs();
//        String fileName = VtuzxUtil.random(8) + "." + VtuzxUtil.first(vFile.ext(), "jpg");
//        vFileList.get(0).file().renameTo(new File(testroomDateFolder + fileName));

    }

//    @Override
//    public void uploadReport(ObjectId userId, String name, String billNo, List<VtuzxFile> vFileList) {
//        VtuzxFile vFile = vFileList.get(0);
//        String dateFolder = VtuzxUtil.formatYmd(new Date());
//        String testroomRootFolder = config.rootPath() + "testroom/";
//        String testroomDateFolder = testroomRootFolder + dateFolder + "/";
//        new File(testroomDateFolder).mkdirs();
//        String fileName = VtuzxUtil.random(8) + "." + VtuzxUtil.first(vFile.ext(), "jpg");
//        vFileList.get(0).file().renameTo(new File(testroomDateFolder + fileName));
//        testRoomDao.updateOneRedirect(Criteria.where("billNo").is(billNo), new Document("$addToSet", new Document("pics", dateFolder + "/" + fileName)));
//    }

    @Override
    public void addPrdProcess(ObjectId userId, String name, Map<String, Object> param) {
        String prdProcessName = VtuzxUtil.getString(param, "prdProcessName");
        String prdProcessNo = "S" + new Date().getTime();
        prdProcessDao.insertBill(userId, name, new Document("prdProcessName", prdProcessName).append("prdProcessNo", prdProcessNo));
    }

    @Override
    public void updatePrdProcess(ObjectId userId, String name, Map<String, Object> param, List<VtuzxFile> vFileList) {
        String number = VtuzxUtil.getString(param, "number");
        String prdProcessName = VtuzxUtil.getString(param, "prdProcessName");
        String detailName = VtuzxUtil.getString(param, "detailName");
        String content = VtuzxUtil.getString(param, "content");
        VtuzxFile vFile = vFileList.get(0);
        String dateFolder = VtuzxUtil.formatYmd(new Date());
        String prdProcessRootFolder = config.rootPath() + "prdProcess/";
        String prdProcessDateFolder = prdProcessRootFolder + dateFolder + "/";
        new File(prdProcessDateFolder).mkdirs();
        String fileName = VtuzxUtil.random(8) + "." + VtuzxUtil.first(vFile.ext(), "jpg");
        vFileList.get(0).file().renameTo(new File(prdProcessDateFolder + fileName));
        prdProcessDao.insertOne(new Document("pic", dateFolder + "/" + fileName)
                .append("number", number)
                .append("detailName", detailName)
                .append("content", content)
                .append("prdProcessName", prdProcessName)
                .append("createUserId", userId)
                .append("createUserName", name));
    }

    @Override
    public void deletePrdProcess(ObjectId userId, String name, Map<String, Object> param) {
        List<String> ids = VtuzxUtil.getObject(param, "ids");
        for (String id: ids) {
            Document prdProcessDoc = prdProcessDao.findById(id);
            String pic = VtuzxUtil.getString(prdProcessDoc, "pic");
            File delFile = new File(config.rootPath() + "prdProcess/" + pic);
            if (delFile.exists()) {
                delFile.delete();
            }
            prdProcessDao.deleteById(id);
        }
    }

    @Override
    public VtuzxMap getPrdProcessList(ObjectId userId, String name, Map<String, Object> param, int pageIndex, int pageSize) {
        String prdProcessName = VtuzxUtil.getString(param, "prdProcessName");
        Criteria cond = new Criteria();
        if (!VtuzxUtil.isEmpty(prdProcessName)) {
            cond.and("prdProcessName").regex(prdProcessName);
        }
        VtuzxPage page = prdProcessDao.page(cond, new Document("createTime", -1), pageIndex, pageSize);
        List<VtuzxMap> dataList = page.iterator(doc -> new VtuzxMap("id", VtuzxUtil.getString(doc, IVtuzxConst.Key__id))
                .append("number", VtuzxUtil.getString(doc, "number"))
                .append("detailName", VtuzxUtil.getString(doc, "detailName"))
                .append("_createUserId", VtuzxUtil.getString(doc, "_createUserId"))
                .append("createUserName", VtuzxUtil.getString(doc, "createUserName"))
                .append("content", VtuzxUtil.getString(doc, "content"))
                .append("prdProcessName", VtuzxUtil.getString(doc, "prdProcessName"))
                .append("pic", VtuzxUtil.getString(doc, "pic"))
                .append("createTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "createTime")))
                .append("updateTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "updateTime"))));

        for (VtuzxMap data: dataList) {
            String pic = VtuzxUtil.getString(data, "pic");
            String certificateDateFolder = config.rootUrl() + "prdProcess/" + pic;
            data.append("pic", certificateDateFolder);
        }

        return new VtuzxMap(IVtuzxConst.Key_total, page.total()).append(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public VtuzxMap getPrdProcessListKey(ObjectId userId, String name, String key) {
        Criteria cond = new Criteria();
        if (!VtuzxUtil.isEmpty(key)) {
            cond.and("prdProcessName").regex(key);
        }
        List<VtuzxMap> dataList = new ArrayList<>();
        Iterator<Document> prdProcessItr = prdProcessDao.findList(cond, null, 0,0);
        while (prdProcessItr.hasNext()) {
            Document prdProcessDoc = prdProcessItr.next();
            VtuzxMap data = new VtuzxMap();
            String prdProcessName = VtuzxUtil.getString(prdProcessDoc, "prdProcessName");
            data.append("prdProcessName", prdProcessName);
            if (!dataList.contains(data)) {
                dataList.add(data);
            }
        }

        return new VtuzxMap(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public void addCertificate(ObjectId userId, String name, Map<String, Object> param, List<VtuzxFile> vFileList) {
        String id = VtuzxUtil.getObject(param, "id");
        String prdBigType = VtuzxUtil.getString(param, "prdBigType");
        String prdSmallType = VtuzxUtil.getString(param, "prdSmallType");
        String prdLicence = VtuzxUtil.getString(param, "prdLicence");
        String prdProcessName = VtuzxUtil.getString(param, "prdProcessName");
        String checkResult = VtuzxUtil.getString(param, "checkResult");
        String dueDate = VtuzxUtil.getString(param, "dueDate");
        String prdStandardNo = VtuzxUtil.getString(param, "prdStandardNo");
        String picStr = VtuzxUtil.getString(param, "picList");
        List<String> picList = new ArrayList<>();
        if (picStr != null) {
            picList = JSON.parseArray(picStr, String.class);
        }
        //文件名称
        String dateFolder = VtuzxUtil.formatYmd(new Date());
        String certificateRootFolder = config.rootPath() + "certificate/";
        String certificateDateFolder = certificateRootFolder + dateFolder + "/";
        new File(certificateDateFolder).mkdirs();
        List<String> uploadPics = new ArrayList<>();
        if (vFileList.size() != 0) {
            for (VtuzxFile vFile: vFileList) {
                String fileName = VtuzxUtil.random(8) + "." + VtuzxUtil.first(vFile.ext(), "jpg");
                vFile.file().renameTo(new File(certificateDateFolder + fileName));
                uploadPics.add(dateFolder + "/" + fileName);
            }
        }

        Document document = new Document("prdBigType", prdBigType)
                .append("prdSmallType", prdSmallType)
                .append("prdLicence", prdLicence)
                .append("prdProcessName", prdProcessName)
                .append("checkResult", checkResult)
                .append("dueDate", dueDate)
                .append("prdStandardNo", prdStandardNo);


        if (id == null) {
            document.append("createUserName", name)
                    .append("createUserId", userId)
                    .append("pics", uploadPics);
            certificateDao.insertOne(document);
        } else {
            Document certificateDoc = certificateDao.findById(id);
            //原有图片
            List<String> oldPicList = VtuzxUtil.getObject(certificateDoc, "pics");
            //循环原有图片
            for (String pic: oldPicList) {
                if (!picList.contains(pic)) {
                    File delFile = new File(config.rootPath() + "certificate/" + pic);
                    if (delFile.exists()) {
                        delFile.delete();
                    }
                }
            }
            picList.addAll(uploadPics);
            document.append("pics", picList);
            certificateDao.updateById(id, document);
        }
    }

    @Override
    public VtuzxMap getCertificateList(ObjectId userId, String name, Map<String, Object> param) {
        int pageIndex = VtuzxUtil.getInt(param, "pageIndex");
        int pageSize = VtuzxUtil.getInt(param, "pageSize");
        Criteria cond = new Criteria();
        VtuzxPage page = certificateDao.page(cond, new Document("createTime", -1), pageIndex, pageSize);
        List<VtuzxMap> dataList = page.iterator(doc -> new VtuzxMap("id", VtuzxUtil.getString(doc, IVtuzxConst.Key__id))
                .append("prdBigType", VtuzxUtil.getString(doc, "prdBigType"))
                .append("prdSmallType", VtuzxUtil.getString(doc, "prdSmallType"))
                .append("prdLicence", VtuzxUtil.getString(doc, "prdLicence"))
                .append("prdProcessName", VtuzxUtil.getString(doc, "prdProcessName"))
                .append("checkResult", VtuzxUtil.getString(doc, "checkResult"))
                .append("dueDate", VtuzxUtil.getString(doc, "dueDate"))
                .append("prdStandardNo", VtuzxUtil.getString(doc, "prdStandardNo"))
                .append("pics", VtuzxUtil.getObject(doc, "pics"))
                .append("createUserName", VtuzxUtil.getString(doc, "createUserName"))
                .append("createUserId", VtuzxUtil.getObject(doc, "createUserId"))
                .append("createTime", VtuzxUtil.formatYmdHmsF(VtuzxUtil.getObject(doc, "createTime"))));

        for (VtuzxMap data: dataList) {
            List<String> picList = new ArrayList<>();
            List<String> videoList = new ArrayList<>();//视频url
            List<String> pics = VtuzxUtil.getObject(data, "pics");
            for (String pic: pics) {
                if (pic.endsWith("mp4")){
//                    String certificateDateFolder = config.rootUrl() + "certificate/" + pic;
                    String certificateDateFolder = "http://zs.xiaoqin.com.cn/xqc_doc/certificate/20240611/axEnoa85.mp4";
                    videoList.add(certificateDateFolder);
                } else {
//                    String certificateDateFolder = config.rootUrl() + "certificate/" + pic;
                    String certificateDateFolder = "http://zs.xiaoqin.com.cn/xqc_doc/" + "certificate/" + pic;
                    picList.add(certificateDateFolder);
                }

            }
            data.append("picList", picList)
                    .append("videoList",videoList);
        }

        return new VtuzxMap(IVtuzxConst.Key_total, page.total()).append(IVtuzxConst.Key_data, dataList);
    }

    @Override
    public void deleteCertificateImg(ObjectId userId, String name, Map<String, Object> param) {
        String id = VtuzxUtil.getString(param, "id");
        String pic = VtuzxUtil.getString(param, "pic");
        Document cerDoc = certificateDao.findById(id);
        List<String> pics = VtuzxUtil.getObject(cerDoc, "pics");
        pics.remove(pic);
        certificateDao.updateById(id, new Document("pics", pics));
    }

    @Override
    public void deleteCertificate(ObjectId userId, String name, Map<String, Object> param) {
        String id = VtuzxUtil.getString(param, "id");
        Document certificateDoc = certificateDao.findById(id);
        List<String> pics = VtuzxUtil.getObject(certificateDoc, "pics");
        for (String pic: pics) {
            File delFile = new File(config.rootPath() + "certificate/" + pic);
            if (delFile.exists()) {
                delFile.delete();
            }
        }
        certificateDao.deleteById(id);
    }

    public static VtuzxMap list2Map(List<? extends Map<String, Object>> dataList, String keyCol) {
        VtuzxMap ret = new VtuzxMap();
        for(Map<String, Object> m: dataList) {
            String key = VtuzxUtil.getString(m, keyCol);
            if (!VtuzxUtil.isEmpty(key)) {
                ret.put(key, m);
            }
        }
        return ret;
    }

    public VtuzxMap getOptions(Map<String, Object> param) {
        List<String> keys = VtuzxUtil.getObject(param, "keys");
        Iterator<Document> dictList = dictDao.findList(Criteria.where("key").in(keys).and("effect").is(true), null, 0, 0);
        VtuzxMap retMap = new VtuzxMap();
        while (dictList.hasNext()) {
            Document dict = dictList.next();
            List<VtuzxMap> options = new ArrayList<>();
            String key = VtuzxUtil.getObject(dict, "key");
            retMap.put(key, options);
            List<Document> dbs = VtuzxUtil.getObject(dict, "details");
            if (dbs != null) {
                sortDictDetails(dbs);
                for (Document detail : dbs) {
                    if (VtuzxUtil.getBoolean(detail, "effect")) {
                        VtuzxMap dictDetail = new VtuzxMap("key", VtuzxUtil.getString(detail, "key"))
                                .append("name", VtuzxUtil.getString(detail, "name"))
                                .append("remark", VtuzxUtil.getString(detail, "remark"));
                        options.add(dictDetail);
                    }
                }
            }
        }
        return retMap;
    }

    private void sortDictDetails(List<Document> details) {
        details.sort((o1, o2) -> {
            int sort1 = VtuzxUtil.getInt(o1, "sort");
            String key1 = VtuzxUtil.getString(o1, "key");
            int sort2 = VtuzxUtil.getInt(o2, "sort");
            String key2 = VtuzxUtil.getString(o2, "key");
            return sort1 == sort2 ? key1.compareTo(key2) : sort1 - sort2;
        });
    }

    @Override
    public VtuzxMap downloadTestRoomList(ObjectId userId, String name, Map<String, Object> param) throws Exception {
        VtuzxMap optionParam = new VtuzxMap();
        List<String> optionKeys = new ArrayList<>();
        optionKeys.add("testMode");
        optionParam.append("keys", optionKeys);

        VtuzxMap optionsMap = getOptions(optionParam);
        List<VtuzxMap> testModeOptions = VtuzxUtil.getObject(optionsMap, "testMode");
        VtuzxMap testModeOptionMap = list2Map(testModeOptions, "key");

        List<Map<String, Object>> dataList = VtuzxUtil.getObject(param, "dataList");
        String timestamp = new SimpleDateFormat("yyMMddHHmmss").format(new Date());List<VtuzxExcelSheet> sheets = new ArrayList<>();
        VtuzxExcelSheet sheet0 = new VtuzxExcelSheet(0, "校验室信息列表");
        int col = 0;
        int row = 0;
        VtuzxExcelRow header0 = new VtuzxExcelRow(row++, new VtuzxArrayList<>(
                new VtuzxExcelCell(col++, "货物类型"),
                new VtuzxExcelCell(col++, "批次号"),
                new VtuzxExcelCell(col++, "产品编码"),
                new VtuzxExcelCell(col++, "产品名称"),
                new VtuzxExcelCell(col++, "规格"),
                new VtuzxExcelCell(col++, "供应商"),
                new VtuzxExcelCell(col++, "数量"),
                new VtuzxExcelCell(col++, "单位"),
                new VtuzxExcelCell(col++, "收货人"),
                new VtuzxExcelCell(col++, "收货时间"),
                new VtuzxExcelCell(col++, "检验人"),
                new VtuzxExcelCell(col++, "检验方式"),
                new VtuzxExcelCell(col++, "检验时间"),
                new VtuzxExcelCell(col, "检验结果")
        ));
        sheet0.addRow(header0);

        for(Map<String, Object> data: dataList) {
            String goodsType = VtuzxUtil.getString(data, "goodsType");
            String batchNo = VtuzxUtil.getString(data, "batchNo");
            String materialNo = VtuzxUtil.getString(data, "materialNo");
            String materialName = VtuzxUtil.getString(data, "materialName");
            String materialSpec = VtuzxUtil.getString(data, "materialSpec");
            String supplierName = VtuzxUtil.getString(data, "supplierName");
            String count = VtuzxUtil.getString(data, "count");
            String unit = VtuzxUtil.getString(data, "unit");
            String colUserName = VtuzxUtil.getString(data, "colUserName");
            String colTime = VtuzxUtil.getString(data, "colTime");
            String testUserName = VtuzxUtil.getString(data, "testUserName");
            String testMode = VtuzxUtil.getString(data, "testMode");
            String testTime = VtuzxUtil.getString(data, "testTime");
            String testQualified = VtuzxUtil.getString(data, "testQualified");
            col = 0;

            VtuzxMap testModeMap = VtuzxUtil.getObject(testModeOptionMap, testMode);
            String testModeKey = "";
            if (!VtuzxUtil.isEmpty(testModeMap)) {
                testModeKey = VtuzxUtil.getString(testModeMap, "name");
            }
            VtuzxExcelRow dataRow = new VtuzxExcelRow(row++, new VtuzxArrayList<>(
                    new VtuzxExcelCell(col++, goodsType),
                    new VtuzxExcelCell(col++, batchNo),
                    new VtuzxExcelCell(col++, materialNo),
                    new VtuzxExcelCell(col++, materialName),
                    new VtuzxExcelCell(col++, materialSpec),
                    new VtuzxExcelCell(col++, supplierName),
                    new VtuzxExcelCell(col++, count),
                    new VtuzxExcelCell(col++, unit),
                    new VtuzxExcelCell(col++, colUserName),
                    new VtuzxExcelCell(col++, colTime),
                    new VtuzxExcelCell(col++, testUserName),
                    new VtuzxExcelCell(col++, testModeKey),
                    new VtuzxExcelCell(col++, testTime),
                    new VtuzxExcelCell(col, "qualified".equals(testQualified)? "合格": "未检")
            ));
            sheet0.addRow(dataRow);
        }

        sheet0.setColumnWidth(new VtuzxArrayList<>(
                new int[] { 0, 200 },
                new int[] { 1, 250 },
                new int[] { 2, 250 },
                new int[] { 3, 250 },
                new int[] { 4, 250 },
                new int[] { 5, 250 },
                new int[] { 6, 150 },
                new int[] { 7, 150 },
                new int[] { 8, 250 },
                new int[] { 9, 300 },
                new int[] { 10, 250 },
                new int[] { 11, 250 },
                new int[] { 12, 250 },
                new int[] { 13, 250 }
        ));
        sheets.add(sheet0);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        VtuzxExcel.write(stream, sheets);
        return new VtuzxDownloadBean("检验室信息" + timestamp + ".xlsx", stream);
    }
}
